﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlackBox.Models;
using System.Web.Security;

namespace BlackBox.Controllers
{
    #region Game ViewModel
    public class GameViewModel
	{
		public Category CategoryData { get; set; }
		public IEnumerable<Game> GameListData { get; set; }
		public IEnumerable<ActiveGame> ActiveGameListData { get; set; }
		public Game GameData { get; set; }
		public UserProfiles UserData { get; set; }
		public GameRating RatingData { get; set; }
	}
    #endregion

    public class GameController : Controller
	{

		#region Repositories
		GameRepository repGame = new GameRepository();
        UserRepository repUser = new UserRepository();
		#endregion

		#region PlayGame
		public ActionResult PlayGame()
        {
            var model = repGame.GetAllCategories();

            return View(model);
        }
		#endregion

		#region Category
		public ActionResult Category(int id)
        {
			GameViewModel model = new GameViewModel();

			model.CategoryData = repGame.GetCategoryById(id);
			model.GameListData = repGame.GetGameByCategory(id);

            return View(model);
        }
		#endregion

		#region ListOfActiveGames
		public ActionResult ListofActiveGames()
        {
            var model = repGame.GetAllActiveGames();
            return View(model);
        }
		#endregion

		#region Game
        [Authorize]
		public ActionResult Game(int id)
        {
			GameViewModel model = new GameViewModel();

			model.CategoryData = repGame.GetCategoryByGame(id);
			model.GameData = repGame.GetGameById(id);
			model.UserData = repUser.GetByName(User.Identity.Name);
			model.RatingData = repGame.GetRating(id, model.UserData.UserId);

            //Dropdown list
			List<SelectListItem> Ratings = new List<SelectListItem>();
			{
				Ratings.Add(new SelectListItem { Text = "0", Value = "0" });
				Ratings.Add(new SelectListItem { Text = "1", Value = "1" });
				Ratings.Add(new SelectListItem { Text = "2", Value = "2" });
				Ratings.Add(new SelectListItem { Text = "3", Value = "3" });
				Ratings.Add(new SelectListItem { Text = "4", Value = "4" });
				Ratings.Add(new SelectListItem { Text = "5", Value = "5" });

				ViewData["Ratings"] = Ratings;
			}

            return View(model);
        }
		#endregion

		#region JoinGame
		[Authorize]
        public ActionResult JoinGame(int Id)
        {
			GameViewModel model = new GameViewModel();
			model.GameData = repGame.GetGameById(Id);
			model.CategoryData = repGame.GetCategoryByGame(Id);
			model.ActiveGameListData = repGame.GetActiveGamesOfType(Id);
            return View(model);
        }
		#endregion

        #region Rate
        public ActionResult Rate(GameViewModel model)
        {
            repGame.AddRating(model.RatingData.Rating, model.GameData.id, model.UserData.UserId);
            //Redirecting the user back to his game
            return RedirectToAction("/Game/" + model.GameData.id);
        }
        #endregion

		#region AddGame
		public ActionResult AddGame()
        {
            Game model = new Game();

            //Dropdown list
            List<SelectListItem> type = new List<SelectListItem>();
            {
                type.Add(new SelectListItem { Text = "Role-Playing ", Value="1" });
                type.Add(new SelectListItem { Text = "Brain ", Value = "2" });
                type.Add(new SelectListItem { Text = "Action ", Value = "3" });
                type.Add(new SelectListItem { Text = "Arcade ", Value = "4" });
                type.Add(new SelectListItem { Text = "Shooting ", Value = "5" });
                type.Add(new SelectListItem { Text = "Sports ", Value = "6" });
                type.Add(new SelectListItem { Text = "Misc ", Value = "7" });

                ViewData["Type"] = type;
            }
            return View(model);
        }
		#endregion

        #region AddGameGameCompleted
        //Add the Game to the database
        [HttpPost]
		public ActionResult AddGameCompleted(Game gameToBeAdded)
        {
            //The rating starts as 0
            gameToBeAdded.rating = 0;
            //Geting the id for the type
            int id = Convert.ToInt32(gameToBeAdded.type);
            //Adding the Game
            repGame.AddNewGame(gameToBeAdded);
            Game game = repGame.GetGameByName(gameToBeAdded.name);
            //Add game to Category
            repGame.AddNewGameToCategory(id, game.id);
            //Redirect to the main page
            return RedirectToAction("Main","Home");

        }
		#endregion

		#region CreateGame
		[Authorize]
        public ActionResult CreateGame(int id)
        {
            //Geting the game by id
            var Game = repGame.GetGameById(id);

            //Creating making a Active Game
            ActiveGame model = new ActiveGame
            {
                gameid = Game.id,
                maxplayers = Game.maxplayers,
                minplayers = Game.minplayers,

            };

            if (model != null)
            {
                //Drop Down list
                List<SelectListItem> gameTime = new List<SelectListItem>();
                {
                    gameTime.Add(new SelectListItem { Text = "No Limit", Value = "1" });
                    gameTime.Add(new SelectListItem { Text = "10 minutes", Value = "2" });
                    gameTime.Add(new SelectListItem { Text = "30 minutes", Value = "3" });
                    gameTime.Add(new SelectListItem { Text = "60 minutes", Value = "4" });

                    ViewData["GameTime"] = gameTime;
                }

                List<SelectListItem> PlayerTime = new List<SelectListItem>();
                {
                    PlayerTime.Add(new SelectListItem { Text = "No Limit", Value = "1" });
                    PlayerTime.Add(new SelectListItem { Text = "10 seconds", Value = "2" });
                    PlayerTime.Add(new SelectListItem { Text = "30 seconds", Value = "3" });
                    PlayerTime.Add(new SelectListItem { Text = "1 minute", Value = "4" });
                    PlayerTime.Add(new SelectListItem { Text = "3 minute", Value = "5" });
                    PlayerTime.Add(new SelectListItem { Text = "10 minute", Value = "6" });

                    ViewData["PlayerTime"] = PlayerTime;
                }
            }

            return View(model);
        }
		#endregion

		#region Create
		[HttpPost]
        public ActionResult Create(ActiveGame newGame)
        {
            newGame.open = true;
            newGame.numberofplayers = 1;

            //Adding the active game 
            if (!repGame.OwnerHasGame(newGame.owner))
            {
                repGame.AddActiveGame(newGame);
                newGame.id = repGame.GetIdByOwner(newGame);
                return RedirectToAction("Play/" + newGame.id);
            }
            else
            {
                return View("Error");
            }
        }
		#endregion

		#region Play
		[Authorize]
        public ActionResult Play(int id)
        {
            var model = repGame.GetActiveGameById(id);

            return View(model);
        }
		#endregion

        #region Get Rating
        public ActionResult GetRatingOfGame(int gameId)
        {
            return RedirectToAction("Main", "Home");
        }
        #endregion
    }
}